import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d

# 生成虚拟数据
np.random.seed(42)
time_stamps = np.sort(np.random.choice(range(0, 100), size=15, replace=False))  # 时间点
temperature_group_1 = np.random.uniform(20, 30, size=len(time_stamps))  # 区域1温度
temperature_group_2 = np.random.uniform(25, 35, size=len(time_stamps))  # 区域2温度

# 创建DataFrame
df = pd.DataFrame({
    'Time': time_stamps,
    'Temperature_Group_1': temperature_group_1,
    'Temperature_Group_2': temperature_group_2
})

# 分段常数插值
time_full = np.arange(0, 100)  # 定义插值后的完整时间范围
f_const_1 = interp1d(df['Time'], df['Temperature_Group_1'], kind='previous', fill_value="extrapolate")
f_const_2 = interp1d(df['Time'], df['Temperature_Group_2'], kind='previous', fill_value="extrapolate")

temperature_interpolated_1 = f_const_1(time_full)
temperature_interpolated_2 = f_const_2(time_full)

# 计算区域平均温度
mean_temp_1 = np.mean(temperature_interpolated_1)
mean_temp_2 = np.mean(temperature_interpolated_2)

# 绘图
plt.figure(figsize=(12, 8))

# 图1：原始数据点
plt.subplot(3, 1, 1)
plt.plot(df['Time'], df['Temperature_Group_1'], 'o-', label="Group 1 - Original", color='blue')
plt.plot(df['Time'], df['Temperature_Group_2'], 'o-', label="Group 2 - Original", color='green')
plt.xlabel("Time")
plt.ylabel("Temperature (°C)")
plt.title("Original Temperature Data Points")
plt.legend()
plt.grid(True)

# 图2：分段常数插值
plt.subplot(3, 1, 2)
plt.step(time_full, temperature_interpolated_1, where='post', label="Group 1 - Step Interpolated", color='blue')
plt.step(time_full, temperature_interpolated_2, where='post', label="Group 2 - Step Interpolated", color='green')
plt.xlabel("Time")
plt.ylabel("Temperature (°C)")
plt.title("Temperature Data with Stepwise Constant Interpolation")
plt.legend()
plt.grid(True)

# 图3：区域平均温度对比
plt.subplot(3, 1, 3)
plt.bar(['Group 1', 'Group 2'], [mean_temp_1, mean_temp_2], color=['blue', 'green'])
plt.xlabel("Group")
plt.ylabel("Average Temperature (°C)")
plt.title("Average Temperature Comparison After Interpolation")
plt.grid(True)

# 调整布局和显示
plt.tight_layout()
plt.show()